const Notice = require('../models/Notice');

const getNoticeInfo = async (noticeId) => {
   return Notice.findById(noticeId)
      .populate({
         path: 'group',
         model: 'Group',
         select: 'group_name',
      })
      .populate({
         path: 'sender',
         model: 'User',
         select: 'username',
      });
};

const makeInvitations = async (groupId, userId, members) => {
   await Notice.insertMany([
      // 1. 发送邀请的通知
      {
         type: 'SEND_INVITATION',
         group: groupId,
         receivers: members,
         sender: userId,
         status: 'read',
      },
      // 2. 拿到邀请的通知
      ...members.map((id) => ({
         type: 'RECEIVE_INVITATION',
         group: groupId,
         sender: userId,
         receiver: id,
      })),
   ]);
};

module.exports = {
   getNoticeInfo,
   makeInvitations,
};
